from visual import *

class SteeringBehaviours():
    def __init__(self, ship):
        self.ship = ship
        self.onArrive = 0
        self.onFlee = 0
        self.onSeek = 1

    def seek(self, targetPos):
       DesiredVelocity = norm(targetPos - self.ship.location)*self.ship.MAX_speed
       return(DesiredVelocity-self.ship.velocity)
   
    def flee(self, targetPos):
       DesiredVelocity = norm(self.ship.location - targetPos)*self.ship.MAX_speed
       return(DesiredVelocity-self.ship.velocity)

    def arrive(self):
        toTarget = self.target - self.agent.position

        dist = mag(toTarget)
        
        if dist > 0:
        
            speed = dist / 3
                        
            if speed > self.agent.speedMAX:
                speed = self.agent.speedMAX
            
            resVelocity = toTarget * speed / dist
            
            return (resVelocity - self.agent.velocity)
        
        return vector(0,0,0)
    
    def alignment(self):
        pass
    
    def orient(self):
        pass
    
    def Calculate(self, targetPos):
        steeringForce = vector(0,0,0)
        
        if self.onArrive:
            #print 'arrive'
            steeringForce += self.arrive()
        if self.onFlee:
            steeringForce += self.flee(targetPos)
        if self.onSeek:
            steeringForce += self.seek(targetPos)

        return self.Truncate(self.agent, steeringForce)
    
    def Truncate(self, mover, vector):
        new = vector
        
        if mag(vector) > mover.speedMAX:
            new = norm(vector) * mover.speedMAX  
            
        return new   
    
    def min(self):  
        if speed > self.agent.speedMAX:
            return self.agent.speedMAX
        else:
            return speed
        
        
        
    
    